package android.example.imageandanimation;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.content.res.AssetManager;
import java.io.InputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.io.FileInputStream;
import 	android.content.res.Resources;

public class Animation{
	
	private String image_path;
	private int sprite_set_start_x, sprite_set_start_y;
	private int frame_count;
	private int frame_width, frame_height;
	private Bitmap sprite_set;	
	private InputStream inputstream;
	private AssetManager assetmanager;
	private Bitmap[] frame;
	private int current_frame;
	private int frame_delay;
	private File sprite_set_file;
	
	
	private AssetManager assetManager;
	public Animation(Bitmap sprite_set, int sprite_set_start_x, int sprite_set_start_y, int frame_count, int frame_width, int frame_height){
		this.sprite_set = sprite_set;
		this.sprite_set_start_x = sprite_set_start_x;
		this.sprite_set_start_y = sprite_set_start_y;
		this.frame_count = frame_count;
		this.frame_width = frame_width;
		this.frame_height = frame_height;
		current_frame = 0;
		frame_delay = 10;		
		frame = new Bitmap[frame_count];
		for(int i = 0; i < frame_count; i ++){
			frame[i] = Bitmap.createBitmap(sprite_set, i*68, 0, 68, 66);
		}
		
		//code load Bitmap from this class
		// sprite_set_file = new File(image_path);
		// try{
		// inputstream = assetManager.open("ryu_anim.png");
		// } catch (IOException e) { 
			 // silently ignored, bad coder monkey, baaad! 
		// } finally { 
			 // we should really close our input streams here. 
		// }
		// sprite_set = BitmapFactory.decodeStream(inputstream);
		// sprite_set = BitmapFactory.decodeFile(image_path);//no need to use inputstream
		// if(sprite_set == null){
			// System.out.println("NULlllllllllllll sprite_set");
		// }	
	}
	
	public void render(Canvas canvas, int render_posx, int render_posy){
	
		// canvas.drawText("Current Frame: " + current_frame, render_posx, render_posy, paint);//need Paint to draw text		
		canvas.drawBitmap(frame[current_frame], render_posx, render_posy, null);
		if(frame_delay == 0){//use frame time instear delay all frame
			frame_delay = 10;
			current_frame ++;
		}else{
			frame_delay --;
		}
		if(current_frame >= frame_count)
			current_frame = 0;
	}
	
}